package com.jiang.mall.mallmember.controller;

import com.jiang.common.exception.BizCodeEnume;
import com.jiang.common.to.LoginTo;
import com.jiang.common.to.SocialUser;
import com.jiang.common.to.UserRegistTo;
import com.jiang.common.utils.PageUtils;
import com.jiang.common.utils.R;
import com.jiang.mall.mallmember.entity.MemberEntity;
import com.jiang.mall.mallmember.exception.PhoneException;
import com.jiang.mall.mallmember.exception.UsernameException;
import com.jiang.mall.mallmember.feign.CouponFeignService;
import com.jiang.mall.mallmember.service.MemberService;
import com.jiang.mall.mallmember.vo.MemberUserRegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author zhangsan
 * @email 2469939264@qq.com
 * @date 2021-03-23 16:50:37
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    @PostMapping("/register")
    public R register(@RequestBody UserRegistTo userRegistTo) {
        MemberUserRegisterVo memberUserRegisterVo = new MemberUserRegisterVo();
        BeanUtils.copyProperties(userRegistTo, memberUserRegisterVo);
        try{
            memberService.checkUsernameUnique(memberUserRegisterVo.getUserName());
            memberService.checkPhoneUnique(memberUserRegisterVo.getPhone());
            memberService.regist(memberUserRegisterVo);
        }catch (UsernameException e) {
            return R.error(BizCodeEnume.USER_EXIT_EXCEPTION.getCode(), BizCodeEnume.USER_EXIT_EXCEPTION.getMsg());
        }catch (PhoneException e){
            return R.error(BizCodeEnume.PHONE_EXIT_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIT_EXCEPTION.getMsg());
        }
        return  R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody LoginTo loginTo) {
        MemberEntity memberEntity = memberService.login(loginTo);
        if(memberEntity == null) {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
        return R.ok().put("memberEntity", memberEntity);
    }

    @PostMapping("/oauth2/login")
    public R oauth2Login(@RequestBody SocialUser socialUser) {
        MemberEntity memberEntity = memberService.login(socialUser);
        if(memberEntity == null) {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
        return R.ok().put("memberEntity", memberEntity);
    }

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
